﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Core;

namespace BTL
{
    public partial class ThongKe : DevComponents.DotNetBar.Office2007Form
    {
        public ThongKe()
        {
            InitializeComponent();
            
        }
        private DataTable Sach()
        {
            DataTable dt = new System.Data.DataTable();
            SqlConnection con = new SqlConnection(Core.Connectionstring.Con);
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "select * from Sach";
            con.Open();
            com.ExecuteNonQuery();
            SqlDataAdapter adap = new SqlDataAdapter(com);
            adap.Fill(dt);
            con.Close();
            return dt;
        }
        private void TinhToan()
        {

            DataTable dt = new System.Data.DataTable();
            SqlConnection con = new SqlConnection(Core.Connectionstring.Con);
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "select count(MaS) as SoLan from tblCthoadon where MaS=@Ma and MONTH(NgayMuon)=@month and YEAR(NgayMuon)=@Nam";
            con.Open();
            string month = textMonth.Text;
            string year = textYear.Text;
            string ma = listSach.SelectedValue.ToString();
            com.Parameters.Add(new SqlParameter("@month",month));
            com.Parameters.Add(new SqlParameter("@Nam", year));
            com.Parameters.Add(new SqlParameter("@Ma", ma));
            com.ExecuteNonQuery();
            SqlDataAdapter adap = new SqlDataAdapter(com);
            adap.Fill(dt);
            con.Close();
            string count = dt.Rows[0][0].ToString().Trim();
            labelSoLan.Text = count;
        }
        private void ThongKe_Load(object sender, EventArgs e)
        {
            listSach.DataSource = Sach();
            listSach.DisplayMember="TenS";
            listSach.ValueMember = "MaS";
        }

        private void listSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            TinhToan();
        }

        private void textMonth_TextChanged(object sender, EventArgs e)
        {
            TinhToan();
        }

        private void textYear_TextChanged(object sender, EventArgs e)
        {
            TinhToan();
        }
    }
}
